/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ola_cloud_token.h"
#include <stddef.h>
#include <string.h>
#include "cJSON.h"
#include "securec.h"
#include "ola_def.h"
#include "ola_log.h"
#include "ola_error.h"
#include "ola_cloud_def.h"
#include "ola_cloud_common.h"

/* 与云端通信的access token，1为增加字符串结尾符存储 */
static char g_accessToken[CLOUD_ACCESS_TOKEN_LEN + 1] = "";
/* 与云端通信的refresh token，1为增加字符串结尾符存储 */
static char g_refreshToken[CLOUD_REFRESH_TOKEN_LEN + 1] = "";
/* access token 失效时间。单位：ms */
static unsigned long long g_tokenExpireTime = 0; /* 0为初始化值 */


/*
 * 功能: 设置 cloud access token
 * 参数：accessToken，写入token
 * 返回: 0，设置成功
 *       非0，设置失败
 */
int CloudSetAccessToken(const char *accessToken)
{
    int ret = OLA_OK;

    do {
        if (accessToken == NULL) {
            OlaLogWarning("invalid param");
            ret = OLA_COM_ERR_INVALID_ARGUMENT;
            break;
        }
        if (memcpy_s(g_accessToken, CLOUD_ACCESS_TOKEN_LEN, accessToken, strlen(accessToken)) != EOK) {
            OlaLogError("memcpy error");
            ret = OLA_SECUREC_ERR_MEMCOPY;
            break;
        }
    } while (false);

    return ret;
}

/*
 * 功能: 获取 cloud access token
 * 返回: NULL，无有效 access token
 *       非NULL，有效 access token
 */
char *CloudGetAccessToken(void)
{
    char *ret = NULL;

    /* 0 为长度 */
    if (strlen(g_accessToken) != 0) {
        ret = g_accessToken;
    }

    return ret;
}

/*
 * 功能: 设置 cloud refresh token
 * 参数：refreshToken，写入token
 * 返回: 0，设置成功
 *       非0，设置失败
 */
int CloudSetRefreshToken(const char *refreshToken)
{
    int ret = OLA_OK;

    do {
        if (refreshToken == NULL) {
            OlaLogWarning("invalid param");
            ret = OLA_COM_ERR_INVALID_ARGUMENT;
            break;
        }
        if (memcpy_s(g_refreshToken, CLOUD_REFRESH_TOKEN_LEN, refreshToken, strlen(refreshToken)) != EOK) {
            OlaLogError("memcpy error");
            ret = OLA_SECUREC_ERR_MEMCOPY;
            break;
        }
    } while (false);

    return ret;
}

/*
 * 功能: 获取 cloud refresh token
 * 返回: NULL，无有效 refresh token
 *       非NULL，有效 refresh token
 */
char *CloudGetRefreshToken(void)
{
    char *ret = NULL;

    /* 0 为长度 */
    if (strlen(g_refreshToken) != 0) {
        ret = g_refreshToken;
    }

    return ret;
}

/*
 * 功能: 设置access token 失效时间
 * 参数：expireTime，失效时间 单位：s 函数内部转换为ms
 */
void CloudSetTokenExpireTime(unsigned long long expireTime)
{
    g_tokenExpireTime = expireTime * TIMEOUT_SLEEP_MS;
}

/*
 * 功能: 获取access token 失效时间
 * 返回: 失效时间 单位：ms
 */
unsigned long long CloudGetTokenExpireTime(void)
{
    return g_tokenExpireTime;
}